﻿using Lightworks.Core.Constant;
using Lightworks.System.Models;
using Lightworks.System.Services;
using Microsoft.AspNetCore.Mvc;


namespace Lightworks.Api.Controllers
{
    [ApiController]
    [Route("api/system/dept")]
    public class DeptController : ApiBaseController
    {
        ILogger<DeptController> logger;
        IDeptService _deptService;

        public DeptController(ILogger<DeptController> logger, IDeptService deptService) 
        {
            this.logger = logger;
            this._deptService = deptService;
        }

        [HttpGet("list")]
        public ApiResult List()
        {
            var data = _deptService.GetAll();
            return Ok(data: data);
        }

        [HttpPost("save")]
        public ApiResult Save(Department model) 
        {
            try {
                _deptService.Save(model);
            }catch(Exception ex) 
            {
                logger.LogError(ex, ex.Message);
                return Error("保存失败");
            }
            return Ok();
        }

        [HttpPost("remove")]
        public ApiResult Delete(long[] ids) 
        {
            try
            {
                _deptService.Remove(ids);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                return Error("保存失败");
            }
            return Ok();
        }
    }
}
